﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace Microsoft.Forums.OfflineStore
{
    /// <summary>
    /// Additional Post object functionality 
    /// </summary>
    public partial class Post
    {
        /// <summary>
        /// Occurs when a child is added
        /// </summary>
        public event Action<Guid> ChildAdded;

        /// <summary>
        /// Called when a child  is added.
        /// </summary>
        /// <param name="postIdAdded">The post id added.</param>
        public void OnChildAdded(Guid postIdAdded)
        {
            if (ChildAdded != null)
            {
                ChildAdded(postIdAdded);
            }
        }


        /// <summary>
        /// Detaches the CreatedBy user object and returns it to the caller
        /// This has the effect of stopping this object from automatically being inserted with the Post object
        /// However, it does not remove the foreign key reference so the caller is responsibile to ensure that the returned
        /// User object exists in the database before this Post object is inserted
        /// </summary>
        /// <returns>The detached User object</returns>
        public User DetachCreatedBy()
        {
            User createdBy = this.CreatedBy;
            createdBy.PostsCreated = null;
            createdBy.PostsModified = null;
            this._CreatedById = createdBy.Id;
            this._CreatedBy = default(EntityRef<User>);
            return createdBy;
        }

        /// <summary>
        /// Detaches the ModifiedBy user object and returns it to the caller
        /// This has the effect of stopping this object from automatically being inserted with the Post object
        /// However, it does not remove the foreign key reference so the caller is responsibile to ensure that the returned
        /// User object exists in the database before this Post object is inserted
        /// </summary>
        /// <returns>The detached User object</returns>
        public User DetachModifiedBy()
        {
            if (this.ModifiedBy == null) return null;

            User modifiedBy = this.ModifiedBy;
            modifiedBy.PostsCreated = null;
            modifiedBy.PostsModified = null;
            this._ModifiedById = modifiedBy.Id;
            this._ModifiedBy = default(EntityRef<User>);
            return modifiedBy;
        }

    }
}
